package iwpl.project.servlet;

import iwpl.project.dao.EquipmentsDAO;
import iwpl.project.entities.Equipments;
import iwpl.project.entitymanagerhelper.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Control_Equipment extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Control_Equipment() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public String getSearch(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String sql="";
		String cbosearch=request.getParameter("cboSearch");
		String txtsearch=request.getParameter("txtSearch");
		String fromDate=request.getParameter("txtFrom");
		String toDate=request.getParameter("txtTo");
		
		if(txtsearch != "" && cbosearch=="")
			sql="where EquipmentName like '%"+txtsearch+"%'";
		else if(cbosearch != "" && fromDate!="" && toDate!="" && txtsearch=="")
			sql="where "+cbosearch+" > '"+fromDate+"' and  "+cbosearch+" < '"+toDate+"'";
		else if(txtsearch!="" && cbosearch!="" && fromDate!="" && toDate!="")
			sql="where EquipmentName like '%"+txtsearch+"%' and "+cbosearch+" > '"+fromDate+"' and  "+cbosearch+" < '"+toDate+"'";
		else
			sql="";
		return sql;
	}
	public void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String search=getSearch(request, response);
		Equipments equip=new Equipments();
		EquipmentsDAO dao=new EquipmentsDAO();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); 
		int id=0;
		try {
			String action=request.getParameter("action");
			if(request.getParameter("id")!=null)
				id=Integer.parseInt(request.getParameter("id"));

			//String txtsearch=request.getParameter("txtSearch");
			String name=request.getParameter("txtName");
			String maintain =request.getParameter("txtMaintain");
			String product =request.getParameter("txtProduct");
			String expiration =request.getParameter("txtExpiration");
			
			equip.setId(id);
			
			if(action.equals("delete")){
				EntityManagerHelper.beginTransaction();
				dao.delete(equip);
				EntityManagerHelper.commit();
				response.sendRedirect("../Manage_Equipment.jsp");
			}
			else if(action.equals("search"))
			{
				/*String cbosearch=request.getParameter("cboSearch");
				String txtsearch=request.getParameter("txtSearch");
				String fromDate=request.getParameter("txtFrom");
				String toDate=request.getParameter("txtTo");
				java.util.Date date4;
				java.util.Date date5;
				java.sql.Timestamp timest4;
				java.sql.Timestamp timest5;
				List<Equipments> list;
				if(fromDate!="" && toDate!=""){
					 date4 = sdf.parse(fromDate);
					 date5 = sdf.parse(toDate);
					 timest4 = new java.sql.Timestamp(date4.getTime());
					 timest5 = new java.sql.Timestamp(date5.getTime());
					 list= dao.findEquipment(txtsearch, cbosearch, timest4, timest5);
					 HttpSession session = request.getSession(true);
					 session.setAttribute("phuoc", list);
				}
				if(txtsearch!=""){
					list=dao.findEquipment(txtsearch);
					//list=dao.findByEquipmentName(txtsearch);
					HttpSession session = request.getSession(true);
					session.setAttribute("phuoc", list);
				}
				if(cbosearch=="" && txtsearch=="" && fromDate=="" && toDate==""){
					list=dao.findAll();
					HttpSession session = request.getSession(true);
					session.setAttribute("phuoc", list);
				}*/
				
				//List<Equipments> list= dao.findAll();
				List<Equipments> list;
				list=dao.findEquipment(getSearch(request, response));
				HttpSession session = request.getSession(true);
				session.setAttribute("phuoc", list);
				response.sendRedirect("../Manage_Equipment.jsp");
			}
				
			else
			{
				
				
				java.util.Date date1 = sdf.parse(maintain); 
				java.util.Date date2 = sdf.parse(product); 
				java.util.Date date3 = sdf.parse(expiration);
				
				
				java.sql.Timestamp timest1 = new java.sql.Timestamp(date1.getTime());
				java.sql.Timestamp timest2 = new java.sql.Timestamp(date2.getTime());
				java.sql.Timestamp timest3 = new java.sql.Timestamp(date3.getTime());
				
				
				equip.setEquipmentName(name);
				equip.setMaintainDate(timest1);
				equip.setProducDate(timest2);
				equip.setEnpiratyDate(timest3);
				
				if(action.equals("insert")){
					EntityManagerHelper.beginTransaction();
					dao.save(equip);
					EntityManagerHelper.commit();
					response.sendRedirect("../Insert_Equipment.jsp");
				}
				
				else
				{
					EntityManagerHelper.beginTransaction();
					dao.update(equip);
					EntityManagerHelper.commit();
					response.sendRedirect("../Manage_Equipment.jsp");
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		}
		
		
		
		
	}
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
		doProcess(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
		doProcess(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	@Override
	public void init() throws ServletException {
		// Put your code here
	}

}
